package com.linghang.wusthelpercompetition.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.linghang.wusthelpercompetition.constant.StatusConstant;
import com.linghang.wusthelpercompetition.dto.CompetitionDTO;
import com.linghang.wusthelpercompetition.dto.CompetitionPostPageDTO;
import com.linghang.wusthelpercompetition.dto.CompetitionPostUpdateDTO;
import com.linghang.wusthelpercompetition.entity.CompetitionPost;
import com.linghang.wusthelpercompetition.mapper.CompetitionPostMapper;
import com.linghang.wusthelpercompetition.result.PageResult;
import com.linghang.wusthelpercompetition.result.Result;
import com.linghang.wusthelpercompetition.service.CompetitionPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

@Service
public class CompetitionPostServiceImpl implements CompetitionPostService {
    @Autowired
    private CompetitionPostMapper competitionPostMapper;


    /**
     *新建一个帖子
     * @param competitionDTO
     */
    @Override
    public void save(CompetitionDTO competitionDTO) {
        //String competitionName = competitionDTO.getCompetitionName();

        //调用builder构建对象
        CompetitionPost competitionPost = CompetitionPost.builder()
                .studentId(competitionDTO.getStudentId())
                .competitionName(competitionDTO.getCompetitionName())
                .competitionIntroduction(competitionDTO.getCompetitionIntroduction())
                .requirement(competitionDTO.getRequirement())
                .contactInformation(competitionDTO.getContactInformation())
                .status(StatusConstant.ENABLE)
                .endUpdateTime(LocalDateTime.now())
                .build();
        competitionPostMapper.insert(competitionPost);
    }

    /**
     * 删除一个帖子
     * @param cid
     */
    @Override
    public void deleteByCid(long cid) {
        competitionPostMapper.deleteByCid(cid);
    }

    /**
     * 更新一个帖子
     * @param competitionPostUpdateDTO
     */
    @Override
    public void update(CompetitionPostUpdateDTO competitionPostUpdateDTO) {
        //调用builder构建对象
        CompetitionPost competitionPost = CompetitionPost.builder()
                .cid(competitionPostUpdateDTO.getCid())
                .competitionName(competitionPostUpdateDTO.getCompetitionName())
                .competitionIntroduction(competitionPostUpdateDTO.getCompetitionIntroduction())
                .requirement(competitionPostUpdateDTO.getRequirement())
                .contactInformation(competitionPostUpdateDTO.getContactInformation())
                .status(competitionPostUpdateDTO.getStatus())
                .endUpdateTime(LocalDateTime.now())
                .build();
        competitionPostMapper.update(competitionPost);
    }

    /**
     * 分页查询
     * @param competitionPostPageDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CompetitionPostPageDTO competitionPostPageDTO) {
        PageHelper.startPage(competitionPostPageDTO.getPage(),competitionPostPageDTO.getPageSize());
        Page<CompetitionPost> page = competitionPostMapper.pageQuery(competitionPostPageDTO);
        long total = page.getTotal();
        List<CompetitionPost> records = page.getResult();
        return new PageResult(total,records);
    }

    @Override
    public CompetitionPost getByCid(Long cid) {
        return competitionPostMapper.getByCid(cid);
    }

    /**
     * 帖子超时15天自动删除帖子
     * @return
     */
    @Override
    public void timeoutPost() {
        List<CompetitionPost>list = competitionPostMapper.getAllPost();
        for(CompetitionPost competitionPost:list){
            LocalDateTime endUpdateTime = competitionPost.getEndUpdateTime();
            LocalDateTime now = LocalDateTime.now();
            Long days = ChronoUnit.DAYS.between(endUpdateTime, now);
            if(days > 15){
                competitionPostMapper.deleteByCid(competitionPost.getCid());
            }
        }
    }

    @Override
    public List<CompetitionPost> selectBycids(List<Long> cids) {
        List<CompetitionPost> competitionPosts = new ArrayList<>();
        cids.forEach(cid -> {
            CompetitionPost byCid = competitionPostMapper.getByCid(cid);

            if(byCid!=null){
                competitionPosts.add(byCid);
            }

        }
        );
        return competitionPosts;
    }

    @Override
    public Long getTotalByStudentId(String studentId) {
        return competitionPostMapper.getTotalByStudentId(studentId);
    }

}
